import { getCountry } from '@/api/order'
export default {
  components: {},
  data() {
    return {
      addressData: {
        province: '',
        city: '',
        county: '',
        cityCode: '',
        provinceCode: '',
        countyCode: ''
      },
      provinces: [], // 省份列表
      citys: [], // 城市列表
      regions: [] // 地区列表
    }
  },
  methods: {
    /**
     * 获取省份列表数据
     */
    apiGetCountryList(type) {
      console.log('getCountry', getCountry)
      getCountry({ code: '0' })
        .then(res => {
          this.provinces = res
          if (type === 'init') return res // 首次初始化不需要清空
          this.citys = []
          this.regions = []
          this.addressData.cityCode = ''
          this.addressData.countyCode = ''
          return res
        })
        .catch(() => {})
    },
    /**
     * 获取城市列表数据
     */
    apiGetCityList(type) {
      const { provinceCode } = this.addressData
      if (!provinceCode) return
      getCountry({ code: provinceCode })
        .then(res => {
          this.citys = res
          if (type === 'init') return res // 首次初始化不需要清空
          this.regions = []
          this.addressData.cityCode = ''
          this.addressData.countyCode = ''
          return res
        })
        .catch(() => {})
    },
    /**
     * 获取地区区列表数据
     */
    apiGetRegionList(type) {
      const { cityCode } = this.addressData
      if (!cityCode) return
      getCountry({ code: cityCode })
        .then(res => {
          this.regions = res
          if (type === 'init') return res // 首次初始化不需要清空
          this.addressData.countyCode = ''
          return res
        })
        .catch(() => {})
    },
    /**
     * 根据省市区code码 查找省市区名字 province city county
     */
    fillName() {
      setTimeout(() => {
        const { provinceCode, cityCode, countyCode } = this.addressData
        const { provinces, citys, regions } = this
        /* eslint-disable*/
        const P = _.find(provinces, { code: provinceCode })
        this.addressData.province = P ? P.fullName : ''
        /* eslint-disable*/
        const C = _.find(citys, { code: cityCode })
        this.addressData.city = C ? C.fullName : ''
        /* eslint-disable*/
        const R = _.find(regions, { code: countyCode })
        this.addressData.county = R ? R.fullName : ''
        return this.addressData
      }, 100)
    },
    /**
     * 根据省市区名字 查找省市区code p省 c市 c区 
     */
   async fillCode(provinceName, cityName, regionName) {
     await Promise.all([this.apiGetCountryList('init'), this.apiGetCityList('init'), this.apiGetRegionList('init')])
      const { provinces,citys, regions} = this
      /* eslint-disable*/
      const P = _.find(provinces, {fullName: provinceName})
      this.addressData.province = P ? P.code : ''
      /* eslint-disable*/
      const C = _.find(citys, {fullName: cityName})
      this.addressData.city = C ? C.code : ''
      /* eslint-disable*/
      const R = _.find(regions, {fullName: regionName})
      this.addressData.county = R ? R.code : ''
      return this.addressData
    }
  }
}
